<div id="app" v-cloak>
    <canvas id="canvas" style="position: absolute;top: 0;left: 0"></canvas>

    <div class="wrapper">
        <div class="login-box">
            <div class="title">系统登录</div>

            <el-form class="content" :model="form" :rules="rules" ref="form">
                <el-form-item prop="username">
                    <el-input placeholder="用户名" v-model="form.username" size="small" prefix-icon="el-icon-user"></el-input>
                </el-form-item>

                <el-form-item prop="password">
                    <el-input placeholder="密码" v-model="form.password" size="small" type="password" prefix-icon="el-icon-lock"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" style="width: 100%" size="small"  :loading="$isSubmit" :disabled="$isSubmit" @click="submitForm('form')">登录</el-button>
                </el-form-item>
            </el-form>

            <a class="tip">系统登录</a>
        </div>
    </div>

</div>
<style>
    #app {
        width: 100%;
        height: 100%;
        padding: 0;
        margin: 0;
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .wrapper {
        width: 450px;
        height: 400px;
        background: #42424263;
        z-index: 1;
        border-radius: 10px;
        display: flex;
        justify-content: center;
        align-items: center;
        box-shadow: 0 0 10px 0 #afafaf52;
    }

    .wrapper .login-box {
        display: inline-block;
        background: #fff;
        border-radius: 5px;
        text-align: left;
    }

    .wrapper .login-box .title {
        line-height: 50px;
        border-bottom: 1px solid #e8e8e8;
        font-weight: bold;
        text-align: left;
        padding-left: 20px;
        font-size: 15px;
        height: 50px;
        color: #757575;
    }

    .wrapper .login-box .content {
        width: 250px;
        margin-top: 20px;
        margin-left: 20px;
        margin-right: 20px;
    }

    .wrapper .login-box .el-form-item{
        margin-bottom: 15px;
    }

    .wrapper .login-box .tip {
        padding: 5px;
        display: inline-block;
        text-align: center;
        color: #b7b7b7;
        width: 100%;
        margin-bottom: 10px;
    }

</style>

<script type="text/javascript">
    var app = new Vue({
        mixins: [mixinForMgr],
        el: "#app",
        data: {
            form: {
                username: '',
                password: ''
            },
            rules: {
                username: [
                    {required: true, message: '请输入用户名', trigger: 'blur'}
                ],
                password: [
                    {required: true, message: '请输入密码', trigger: 'change'}
                ]
            }
        },
        mounted: function () {
            var canvas = document.getElementById("canvas");
            var context = canvas.getContext("2d");

            // 设置全屏
            var o = document.getElementById("app");
            var width = o.offsetWidth;
            var height = o.offsetHeight;
            canvas.width = width;
            canvas.height = height;

            var stars = [];
            var p = 30;
            for (var i = 0; i < width; i += p) {
                for (var j = 0; j < height; j += p) {
                    stars.push({
                        x: p * 1.5 * Math.random() + i,
                        y: p * 1.5 * Math.random() + j,
                        r: 0.8 * Math.random(),
                        a: Math.random() + 0.1,
                        c: Math.random() > 0.7
                    });
                }
            }

            var v = 0;
            setInterval(function () {
                //设置背景色
                context.clearRect(0, 0, width, height);
                context.fillStyle = "#091044";
                context.fillRect(0, 0, width, height);

                var s = Math.sin(v % Math.PI);
                v += Math.PI / 100;
                for (var i = 0; i < stars.length; i++) {
                    var star = stars[i];
                    context.beginPath();
                    context.arc(star.x, star.y, star.r, 0, Math.PI * 2);
                    context.closePath();
                    if (star.c) {
                        context.fillStyle = "rgba(255,255,255," + s * star.a + ")";
                    } else {
                        context.fillStyle = "rgba(255,255,255," + star.a + ")";
                    }
                    context.fill();
                }
            }, 50)
        },
        methods: {
            submitForm: function (formName) {
                this.$refs[formName].validate(function (valid) {
                    if (valid) {
                        this.$isSubmit = true;
                        new Ajax("wsys", "User").method("login").post(this.form, function (response) {
                            this.$isSubmit = false;
                            if (response.errors.length > 0) {
                                nav.e(response.errors[0].message);
                            } else {
                                nav.i("登录成功.", function () {
                                    nav.toHome();
                                });
                            }
                        }.bind(this));
                    } else {
                        return false;
                    }
                }.bind(this));
            },
            resetForm: function (formName) {
                this.$refs[formName].resetFields();
            }
        },
    })
</script>
